package com.efast.cafe.commponent.statemach.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.efast.cafe.commponent.statemach.bean.TaskParamBean;
import com.efast.cafe.commponent.statemach.service.IPortalStateMachTaskService;
import com.efast.cafe.commponent.statemach.service.IStateMachService;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;

@Service
@Transactional
public class StateMachServiceImpl extends StateMachAbstractServiceImpl implements IStateMachService {

	@Autowired
	private IPortalStateMachTaskService portalStateMachTaskService;

	private Long confId;

	private Long taskId;

	public Long getConfId() {
		return confId;
	}

	public void setConfId(Long confId) {
		this.confId = confId;
	}

	public Long getTaskId() {
		return taskId;
	}

	public void setTaskId(Long taskId) {
		this.taskId = taskId;
	}

	public StateMachServiceImpl() {
	}

	public StateMachServiceImpl(Long confId, Long taskId) {
		this.setConfId(confId);
		this.setTaskId(taskId);
	}

	@Override
	public void addListener(Object object, String methodName, Object... args) {
		this.getEventHandler().addEvent(object, methodName, args);
	}

	@Override
	public void execute(Long confId, Long taskId, String parajson,Map<String, JSONObject> extFormDataJson) {
		try {
			// 先执行业务方法
			this.getEventHandler().execute();
		} catch (Exception e) {
			throw new ServiceException(ResultStatus.SYS_ERROR.getCode());
		}
		// 执行状态机
		TaskParamBean tp = new TaskParamBean();
		tp.setConfId(confId);
		tp.setTaskId(taskId);
		tp.setParajson(parajson);
		if (taskId == null || taskId == 0) {
			portalStateMachTaskService.begin(tp,extFormDataJson);
		} else {
			portalStateMachTaskService.operation(tp,extFormDataJson);
		}
		
	}
}
